import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { persistReducer, persistStore } from "redux-persist";
import storage from "redux-persist/lib/storage";
import { languageReducer, recommentdProducts } from "./reducer";
import { changeLanguage } from "./middlewear/changeLanguage";
import { productDetailSlice } from "./detailProduct/detailSlice";
import { languageSlice } from "./languageChangeAndAdd/languageSlice";
import { searchSlice } from "./searchSlice/searchSilce";
import { userSlice } from "./user/userSlice";
import { shoopingCartSlice } from "./shoopingCart/shoopingCartSlice";
import { orderSlice } from "./order/orderSlice";

const rootReducer = combineReducers({
  languageReducer,
  recommentdProducts,
  productDetail: productDetailSlice.reducer,
  languageSlices: languageSlice.reducer,
  searchProduct: searchSlice.reducer,
  user: userSlice.reducer,
  shopingCart: shoopingCartSlice.reducer,
  orderSlice: orderSlice.reducer,
});

// 持久化配置信息
const persistConfig = {
  key: "root",
  storage,
  shiteList: ["user", "shopingCart"], //白名单：将需要持久化的redux的user部分赋予持久化
};

// 配置持久化的reducer
const persistReducers = persistReducer(persistConfig, rootReducer);

const store = configureStore({
  reducer: persistReducers, //声明store
  devTools: true, //打开调试器
  //合并中间件
  middleware: (getDefaultMiddleware) => [
    ...getDefaultMiddleware({ serializableCheck: false }),
    changeLanguage,
  ],
});

// 创建持久化的store
const persistor = persistStore(store);

export type RootState = ReturnType<typeof store.getState>;
export type StateDispatch = ReturnType<typeof store.dispatch>;
export { store, persistor };
